package com.wx.subject.application.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Preconditions;
import com.wx.domain.entity.SubjectAnswerBO;
import com.wx.domain.entity.SubjectInfoBO;
import com.wx.domain.service.SubjectInfoDomainService;
import com.wx.subject.application.convert.SubjectAnswerDTOConverter;
import com.wx.subject.application.convert.SubjectInfoDTOConverter;
import com.wx.subject.application.dto.SubjectInfoDTO;
import com.wx.subject.common.entity.Result;
import com.wx.subject.infra.basic.entity.SubjectCategory;
import com.wx.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @BelongsProject: xx-club
 * @BelongsPackage: com.wx.subject.application.controller
 * @Author: wangx
 * @CreateTime: 2024-03-04  21:48
 * @Description: TODO
 * @Version: 1.0
 */
@RequestMapping("/subject")
@RestController
@Slf4j
public class SubjectController {

    @Resource
    private SubjectCategoryService subjectCategoryService;
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;
    /**
     *  添加分类
     * @param subjectCategory
     * @return
     */
    @PostMapping("/category/add")
    public Result addCategory(@RequestBody SubjectCategory subjectCategory){
        log.info("添加分类：{}",subjectCategory);
        return Result.ok(subjectCategoryService.save(subjectCategory));
    }
    /**
     * 更新
     * @param subjectCategory
     * @return
     */
    @PostMapping("/category/update")
    public Result updateCategory(@RequestBody SubjectCategory subjectCategory){
        return Result.ok(subjectCategoryService.updateById(subjectCategory));
    }
    /**
     *  删除
     * @param  subjectCategory
     * @return
     */
    @PostMapping("/category/delete")
    public Result deleteCategory(@RequestBody SubjectCategory subjectCategory){
            return  Result.ok(subjectCategoryService.deleteById(subjectCategory.getId()));
    }
    /**
     *   查询分类
     * @return
     */
    @PostMapping("/category/queryPrimaryCategory")
    public Result listCategory(@RequestBody SubjectCategory subjectCategory){
        QueryWrapper<SubjectCategory> qw = new QueryWrapper<>();
        qw.eq("category_type",subjectCategory.getCategoryType());
        Page<SubjectCategory> page = subjectCategoryService.page(new Page<>(), qw);
        return  Result.ok(page);
    }
    /**
     *    查询大类下分类
     */
    @PostMapping("/category/queryCategoryByPrimary")
    public Result queryCategoryByPrimary(@RequestBody SubjectCategory subjectCategory){
        QueryWrapper<SubjectCategory> qw = new QueryWrapper<>();
        qw.eq("category_type",subjectCategory.getCategoryType());
        qw.and(Wrapper->Wrapper.eq("parent_id",subjectCategory.getParentId()));
        Page<SubjectCategory> page = subjectCategoryService.page(new Page<>(), qw);
        return  Result.ok(page);
    }

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public Result add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()),
                "题目名称不能为空");
        Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
        Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
        Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
        Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds())
                , "分类id不能为空");
        Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds())
                , "标签id不能为空");

        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
        List<SubjectAnswerBO> subjectAnswerBOS =
                SubjectAnswerDTOConverter.INSTANCE.convertListDTOToBO(subjectInfoDTO.getOptionList());
        subjectInfoBO.setOptionList(subjectAnswerBOS);
        subjectInfoDomainService.add(subjectInfoBO);
        return Result.ok(true);
    }
}

